﻿using System.Collections.Generic;
using System.Linq;
using ML.IBusiness;
using ML.IRepository;
using ML.Entity;

namespace ML.Business
{
    public class PlatFormCategoryBusiness : IPlatFormCategoryBusiness
    {
        private readonly IPlatformCategoryRepository pfcRepository;
        private readonly IMerchantRepository merchantRepository;
        public PlatFormCategoryBusiness(IPlatformCategoryRepository _pfcRepository, IMerchantRepository _merchantRepository)
        {
            pfcRepository = _pfcRepository;
            merchantRepository = _merchantRepository;
        }

        public BaseResult<Pager<PlatformCategoryEntity>> List(PlatFormCategorySearch pfcSearch)
        {
            //根据传递的条件查询
            Pager<PlatformCategoryEntity> data = pfcRepository.List(pfcSearch);
            return new BaseResult<Pager<PlatformCategoryEntity>>(data);
        }

        public BaseResult<IEnumerable<PlatformCategoryEntity>> GetAllEnableList()
        {
            var data = pfcRepository.GetAllEnableList();
            if (data.Count() <= 0)
            {
                return new BaseResult<IEnumerable<PlatformCategoryEntity>>(202);
            }
            return new BaseResult<IEnumerable<PlatformCategoryEntity>>(200, data);
        }

        public BaseResult<IEnumerable<DropDownList>> GetDropDownListList()
        {
            var data = pfcRepository.GetAllEnableList();

            //处理成固定的格式返回
            var result = data.Select(n => new DropDownList()
            {
                value = n.Id,
                name = n.Name
            });
            return new BaseResult<IEnumerable<DropDownList>>(200, result);
        }

        public BaseResult<List<PlatFormCategoryResponse>> GetPageEnableList(int pagesize)
        {
            //首先查询所有数据
            var data = pfcRepository.GetAllEnableList();
            if (data.Count() <= 0)
            {
                return new BaseResult<List<PlatFormCategoryResponse>>(202);
            }
            //查询总数量
            int pageIndex = data.Count() / pagesize + 1;
            List<PlatFormCategoryResponse> lists = new List<PlatFormCategoryResponse>();
            for (var i = 0; i < pageIndex; i++)
            {
                //根据页面查询前面8条数据
                var list = data.Skip(i * pagesize).Take(pagesize);
                lists.Add(new PlatFormCategoryResponse()
                {
                    pageIndex = i + 1,
                    list = list
                });
            }
            return new BaseResult<List<PlatFormCategoryResponse>>(200, lists);
        }

        public BaseResult<bool> Add(PlatformCategoryEntity entity)
        {
            var isTrue = pfcRepository.Add(entity);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201);
        }

        public BaseResult<bool> Update(PlatformCategoryEntity entity)
        {
            var isTrue = pfcRepository.Update(entity);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201);
        }

        public BaseResult<bool> Delete(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new BaseResult<bool>(1001, false);
            }

            //判断平台分类是否设置商户，如果设置商户，则不允许删除
            var data = merchantRepository.GetListByPlatFormId(id, 1, 2);
            if (data.Count() > 0)
            {
                return new BaseResult<bool>(1050, false);
            }

            var isTrue = pfcRepository.Delete(id);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201, false);
        }
    }
}